package com.buttonpay.home;


import java.util.ArrayList;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;

import com.buttonpay.AppData;
import com.buttonpay.ImageLoader;
import com.buttonpay.R;

public class TransactionList extends Activity
{
	ListView list_transaction;
	Button btnBack,btnLogout;
	@Override
	protected void onCreate(Bundle savedInstanceState) 
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.transaction_list);
		TextView tveaderText = (TextView)findViewById(R.id.tv_headerText);
		tveaderText.setText("Transactions List");
		list_transaction=(ListView)findViewById(R.id.list_transactionList);
		btnBack=(Button)findViewById(R.id.btnPrevious);
        btnLogout=(Button)findViewById(R.id.btnLogout);
		MyListAdapter myListAdapter =new MyListAdapter(this, AppData.transactionArray);
		list_transaction.setAdapter(myListAdapter);	
		btnBack.setOnClickListener(new OnClickListener()
		{
			
			public void onClick(View arg0) 
			{
				// TODO Auto-generated method stub
				finish();
			}
			
		});
		
		btnLogout.setOnClickListener(new OnClickListener()
		{
			
			public void onClick(View v) 
			{
				new AlertDialog.Builder(TransactionList.this)
		        .setTitle( "Exit Application" )
		        .setMessage( "Are you sure you want to Logout?" )
		        .setPositiveButton("YES", new android.content.DialogInterface.OnClickListener() 
		        {
		            public void onClick(DialogInterface arg0, int arg1) 
		            {
		                setResult(2);
		            	finish();
		            }

					public void onClick(View v) 
					{
						// TODO Auto-generated method stub 
					}
		        })
		        .setNegativeButton("NO",  new android.content.DialogInterface.OnClickListener() {
		            public void onClick(DialogInterface arg0, int arg1)
		            {
		                //do stuff onclick of CANCEL
		            	arg0.dismiss();
		               
		            }

					public void onClick(View v) 
					{
						// TODO Auto-generated method stub
					}
		        }).show();
				  		
			}
		});
	
	}
	
	public static class ViewHolder
	{
		 public TextView tvProductName,tvRefNo,tvTopUpValue,
							tvResponseDesc,tvRequestTime;
    }
	 
	 class MyListAdapter extends ArrayAdapter<TransactionDetails> 
	 {

			private final Context context;

			LayoutInflater vi ;
			ImageLoader imageLoader;
			ArrayList<TransactionDetails> transactions;
			TransactionDetails transactionDetail;

			public MyListAdapter(Context context,ArrayList<TransactionDetails> transactions)
			{
				super(context, R.layout.row_transaction_details,transactions);
				
				//this.imageLoader=new ImageLoader(context);
				this.context = context; 
				this.transactions=transactions;
				System.out.println("Size:"+this.transactions.size());
				vi = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);

			}

			public View getView(final int position, View convertView, ViewGroup parent) 
			{
				View v = convertView;
				ViewHolder holder = null;
				
				if (v == null) 
				{
					holder=new ViewHolder();  
					v = vi.inflate(R.layout.row_transaction_details, null); 
					holder.tvProductName=(TextView) v.findViewById(R.id.tv_prouctName);
					holder.tvRefNo=(TextView)v.findViewById(R.id.tv_referenceNumber);
					holder.tvTopUpValue=(TextView)v.findViewById(R.id.tv_topUpValue);
					holder.tvRequestTime=(TextView)v.findViewById(R.id.tv_requestTime);
					holder.tvResponseDesc=(TextView)v.findViewById(R.id.tv_responseDesc);
					v.setTag(holder);
					
				}
				else
					holder=(ViewHolder)v.getTag();
					transactionDetail=this.transactions.get(position);
					 
					System.out.println("==>Product Name:"+transactionDetail.getProduct());
					System.out.println("==>Reference No:"+transactionDetail.getReferenceNumber()+
							"\nTop Up Value:"+transactionDetail.getTopUpValue()+
							"\nRequest Time:"+transactionDetail.getRequestTime()+
							"\nResponse Desc:"+transactionDetail.getTransactionResponseDescription());
					
					
					
					holder.tvProductName.setText(transactionDetail.getProduct());
					holder.tvRefNo.setText(transactionDetail.getReferenceNumber());
					holder.tvTopUpValue.setText(transactionDetail.getTopUpValue());
					holder.tvRequestTime.setText(transactionDetail.getRequestTime());
					holder.tvResponseDesc.setText(transactionDetail.getTransactionResponseDescription());
					
					/*if(transactionDetail.getTransactionResponseCode().equalsIgnoreCase("000"))
					{
						holder.tvResponseDesc.setTextColor(ColorStateList.valueOf(R.color.green));
					}*/
					
					return v; 
			}//getView

		}
}
